﻿using System.Collections.Generic;
using Common;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.ObjectBuilder;

namespace CSC440TeamOne.Admin.Views
{
    public class EditHelpTextPresenter : Presenter<IEditHelpTextView>
    {
        private List<Question> AllQuestions;

        // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller
        //       The code will not work in the Shell module, as a module controller is not created by default
        //
        private IAdminController _controller;
        public EditHelpTextPresenter([CreateNew] IAdminController controller)
        {
            _controller = controller;
        }

        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
            AllQuestions = _controller.LoadAllQuestions();
        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads
        }

        
        public List<Question> getAllQuestions()
        {
            return this.AllQuestions;
        }

        public List<string> getQuestionTitles()
        {
            List<string> titles = new List<string>();

            foreach (Question q in AllQuestions)
            {
                titles.Add(q.QuestionID.ToString() + " - " + q.QuestionText);
            }

            return titles;
        }

        public HelpText getHelpText(int questionID)
        {
            HelpText helpText = null;

            foreach (Question q in AllQuestions)
            {
                if (q.QuestionID == questionID)
                {
                    helpText = q.HelpText;
                    break;
                }
            }

            return helpText;
        }

        public void saveHelpText(int questionID, HelpText helpText)
        {
            // Save to current object
            foreach (Question q in AllQuestions)
            {
                if (q.QuestionID == questionID)
                {
                    q.HelpText = helpText;
                    break;
                }
            }

            // Pass upstream to the controller for permanent save
            this._controller.saveHelpText(questionID, helpText);
        }
    }
}




